package com.ihd.ihardware.view.tzc.me.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.RulerView;
import com.ihd.ihardware.databinding.ActivityTargetNewBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;
import com.ihd.ihardware.view.tzc.me.viewmodel.TargetViewModel;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.AppActivityManager;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class TargetActivity extends BaseActivity<ActivityTargetNewBinding, TargetViewModel> {

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("设置目标");
        binding.mtitlebar.setLeftBack(this);

        binding.rulerHeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
            @Override
            public void onValueChange(float value) {
                binding.currWeightTV.setText(Float.toString(value));
            }
        });
        binding.rulerHeight.setValue(50.0f, 30.0f, 180.0f, 0.1f);

        binding.finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.dream(binding.currWeightTV.getText().toString());
            }
        });

        setRx(AppConstans.DREAM, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(TargetActivity.this);
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                viewModel.getUserInfo();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(TargetActivity.this, error.getMessage());
                DialogUtils.dismiss(TargetActivity.this);
            }
        });

        setRx(AppConstans.INFO, new BaseConsumer<UserInfoRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(UserInfoRes mLiveData) {
                DialogUtils.dismiss(TargetActivity.this);
                SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(mLiveData.getData(), UserInfoRes.DataBean.class));
                Intent i = new Intent(TargetActivity.this, BalanceActivity.class);
                startActivity(i);
                AppActivityManager.getInstance().killActivity(PerInfoActivity.class);
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(TargetActivity.this, error.getMessage());
                DialogUtils.dismiss(TargetActivity.this);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_target_new;
    }

    @Override
    protected Class getViewModelClass() {
        return TargetViewModel.class;
    }
}
